<!-- 图片上传组件（多个），封装了预览和删除 -->
<template>
  <div  class="attestation-uploader-mul">
    <el-upload
      ref="uploadMul"
      :accept="accept"
      action="https://jsonplaceholder.typicode.com/posts/"
      :limit="limit"
      :headers="headers"
      list-type="picture-card"
      :before-upload="beforeAvatarUpload"
      :on-exceed="handleExceed">
      <template slot="default">
        <span class="attestation-title" v-if="title">{{title}}</span>
        <i class="el-icon-plus" v-else></i>
      </template>
      <div slot="file" slot-scope="{file}">
        <img
          class="el-upload-list__item-thumbnail"
          :src="file.url" alt=""
        >
        <span class="el-upload-list__item-actions">
            <span
              class="el-upload-list__item-preview"
              @click="handlePictureCardPreview(file)"
            >
              <i class="el-icon-view"></i>
            </span>
            <span
              v-if="!disabled"
              class="el-upload-list__item-delete"
              @click="handleRemove(file)"
            >
              <i class="el-icon-delete"></i>
            </span>
          </span>
      </div>
    </el-upload>
    <el-dialog append-to-body :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>
<script>
import { getToken } from '@/utils/auth'
export default {
  name: "attestationUploadImageMul",
  props: {
    title: {
      type: String,
      default: ''
    },
    limit: {
      type: Number,
      default: 5
    },
    size: {
      type: Number,
      default: 10
    },
    accept: {
      type: String,
      default: '.png, .jpg, .jpeg'
    },
  },
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      headers: { Authorization: 'Bearer ' + getToken() },
      acceptList: this.accept.split('.').join('').split(' ').join('').split(','),
    };
  },
  methods: {
    handleRemove(file) {
      this.$refs.uploadMul.uploadFiles.forEach((v, index) => {
        if (file.name === v.name && file.url === v.url) {
          this.$refs.uploadMul.uploadFiles.splice(index, 1);
        }
      });
    },
    handleExceed(files, fileList) {
      this.$message.error(`上传文件个数不能超过${this.limit}个`);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    beforeAvatarUpload(file) {
      const isJPG = this.acceptList.includes(file.name.substring(file.name.lastIndexOf(".")+1))
      const isLt2M = file.size / 1024 / 1024 < this.size;

      if (!isJPG) {
        this.$message.error(`上传图片只能是${this.acceptList.join("/")}格式`);
      }
      if (!isLt2M) {
        this.$message.error(`上传图片大小不能超过${this.size}MB`);
      }
      return isJPG && isLt2M;
    },
  }
}
</script>
<style rel="stylesheet/scss" lang="scss">
.attestation-uploader-mul {
  height: 90px;
  display: inline-block;
  .avatar-uploader .el-upload {
    border-radius: 6px;
    cursor: pointer;
    position: relative;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar {
    width: 90px;
    height: 90px;
    display: block;
  }
  .el-upload--picture-card .el-icon-plus {
    margin-top: calc(50% - 14px);
  }
  .attestation-title {
    position: absolute;
    left: 50%;
    top: 50%;
    margin: auto;
    width: 100%;
    color: #333333;
    transform: translate(-50%,-50%);
  }
  .el-upload--picture-card {
    width: 92px;
    height: 92px;
    line-height: 16px;
    position: relative;
  }
  .el-upload-list__item {
    width: 90px;
    height: 90px;
  }
}
</style>
